package longrise.phone.com.bjjt_jyb.Activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnTouchListener;

import longrise.phone.com.bjjt_jyb.Application.BaseApplication;
import longrise.phone.com.bjjt_jyb.R;
import longrise.phone.com.bjjt_jyb.Utils.UiUtil;

import android.view.View.OnClickListener;

/**
 * Created by Administrator on 2016/7/13.
 */
public class MyVideoActivity extends Activity implements OnClickListener {


    private MovieRecorderView mRecorderView;//视频录制控件
    private Button mShootBtn;//视频开始录制按钮
    private boolean isFinish = true;
    private boolean success = false;//防止录制完成后出现多次跳转事件
    private Bundle mBundle;
    private String appcaseno;
    private Dialog processDialog;
    private String filePath;
    private BaseApplication mApplication;
    private LinearLayout btn_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myvideo);
        mApplication = (BaseApplication) MyVideoActivity.this.getApplicationContext();
        mRecorderView = (MovieRecorderView) findViewById(R.id.movieRecorderView);
        mShootBtn = (Button) findViewById(R.id.shoot_button);
        btn_back = (LinearLayout) findViewById(R.id.hn_btn_back);
        btn_back.setOnClickListener(this);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("现场视频");
        //用户长按事件监听
        mShootBtn.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {//用户按下拍摄按钮

                    mRecorderView.record(new MovieRecorderView.OnRecordFinishListener() {

                        @Override
                        public void onRecordFinish() {
                            if (!success && mRecorderView.getTimeCount() < 10) {//判断用户按下时间是否大于10秒
                                success = true;
                                handler.sendEmptyMessage(1);
                            }
                        }
                    });
                } else if (event.getAction() == MotionEvent.ACTION_UP) {//用户抬起拍摄按钮
                    if (mRecorderView.getTimeCount() > 3) {//判断用户按下时间是否大于3秒
                        if (!success) {
                            success = true;
                            handler.sendEmptyMessage(1);
                        }
                    } else {
                        success = false;
                        if (mRecorderView.getmVecordFile() != null)
                            mRecorderView.getmVecordFile().delete();//删除录制的过短视频
                            mRecorderView.stop();//停止录制
                            MyVideoActivity.this.finish();
                            MyVideoActivity.this.onDestroy();
                        Toast.makeText(MyVideoActivity.this, "视频录制时间太短，请重新录制", Toast.LENGTH_SHORT).show();
                    }
                }else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
                    //点击了权限按钮
                    mRecorderView.stop();//停止录制
                }
                return true;
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        isFinish = true;
        if (mRecorderView.getmVecordFile() != null)
            mRecorderView.getmVecordFile().delete();//视频使用后删除
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        isFinish = false;
        success = false;
        mRecorderView.stop();//停止录制
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != processDialog) {
            if (processDialog.isShowing()) {
                processDialog.dismiss();
            }
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (success) {
                finishActivity();
            }
        }
    };

    //视频录制结束后，跳转的函数
    private void finishActivity() {
        if (isFinish) {
            isFinish=false;
            processDialog = UiUtil.showProcessDialog(MyVideoActivity.this, "视频拍摄成功，准备上传...");
            filePath = mRecorderView.getmVecordFile().toString();
            new Handler().postDelayed(new Runnable() {
                public void run() {
                    mRecorderView.stop();
                    sendImage(filePath);
                }
            }, 2000);

            /*try {
                Thread.sleep(2000);
                mRecorderView.stop();
                sendImage(filePath);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }*/
        }success=false;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.hn_btn_back:
                onDestroy();
                finish();
                break;
        }
    }

    /**
     * 录制完成回调
     */
    public interface OnShootCompletionListener {
        public void OnShootSuccess(String path, int second);

        public void OnShootFailure();
    }

    /**
     * 返回视频的地址
     *
     * @param path
     */
    private void sendImage(String path) {
        Intent intent = new Intent();
        Bundle b = new Bundle();
        b.putString("file", path);
        intent.putExtras(b);
        this.setResult(MyVideoActivity.RESULT_OK, intent);
        this.onDestroy();
        this.finish();
    }
}
